﻿using Daily.Application.DTO;
using Daily.Infrastructure.Helper;
using DailyApp.Infrastructure.Enums;
using DailyApp.WPF.HttpClients;
using DailyApp.WPF.MsgEvents;
using DailyAPP.Domain.Model;
using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;

namespace DailyApp.WPF.ViewModels
{
    public class LoginUCViewModel : BindableBase,IDialogAware
    {
        private readonly HttpRestClient httpRestClient;
        private readonly IEventAggregator eventAggregator;

        public LoginUCViewModel(HttpRestClient httpRestClient,IEventAggregator eventAggregator)
        {
            LoginCommand = new DelegateCommand(Login);
            TogglePageCommand = new DelegateCommand(TogglePage);
            CloseWindowCommand = new DelegateCommand(CloseWindow);
            RegistorCommand = new DelegateCommand(Registor);
            LoginInputDTOProp = new LoginInputDTO();
            this.httpRestClient = httpRestClient;
            this.eventAggregator = eventAggregator;
        }
        #region 定义方法
        public DelegateCommand LoginCommand { get; set; }
        public DelegateCommand TogglePageCommand { get; set; }
        public DelegateCommand CloseWindowCommand { get; set; }
        public DelegateCommand RegistorCommand { get; set; }
        #endregion

        #region 方法实现
        private async void Login()
        {
            if (string.IsNullOrEmpty(AccountName)  || string.IsNullOrEmpty(Pwd))
            {
                eventAggregator.GetEvent<MsgEvent>().Publish("登录信息不全!");
                return;
            }
            var pw = MD5Helper.MD5Encrypt32(Pwd);

            ApiRequest<string> apiRequest = new ApiRequest<string>();
            apiRequest.Method = RestSharp.Method.GET;
            apiRequest.Route = ApiRouteHelper.GetRoute(ApiRoute.Login) + $"?account={AccountName}&pwd={pw}";
            var result = await httpRestClient.Execute<string, User>(apiRequest);
            if (result.ResultCode == 100)
            {
                eventAggregator.GetEvent<MsgEvent>().Publish(result.Msg);
                DialogParameters pairs=new DialogParameters();
                pairs.Add("UserName", result.Data?.UserName); 
                RequestClose?.Invoke(new DialogResult(ButtonResult.OK, pairs));
            }else
                eventAggregator.GetEvent<MsgEvent>().Publish(result.Msg);

        }
        private async void Registor()
        {
            var input = LoginInputDTOProp;
            //数据基本验证
            if (string.IsNullOrEmpty(input.UserName) || string.IsNullOrEmpty(input.AccountName) || string.IsNullOrEmpty(input.Password) || string.IsNullOrEmpty(input.ConfirmPassword))
            {
                //MessageBox.Show("注册信息不全");
                eventAggregator.GetEvent<MsgEvent>().Publish("注册信息不全!");
                return;
            }
            if (!Regex.IsMatch(input.AccountName, @"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$")) {
                eventAggregator.GetEvent<MsgEvent>().Publish("账号必须为邮箱，请输入正确的邮箱地址!");
                return;
            }
            if (input.ConfirmPassword != input.ConfirmPassword)
            {
                //MessageBox.Show("两次密码输入不一致"); 
                eventAggregator.GetEvent<MsgEvent>().Publish("两次密码输入不一致!");
                return; }
             
            input.Password = MD5Helper.MD5Encrypt32(input.Password);
            input.ConfirmPassword = MD5Helper.MD5Encrypt32(input.ConfirmPassword);

            ApiRequest<LoginInputDTO> apiRequest=new ApiRequest<LoginInputDTO>();
            apiRequest.Method=RestSharp.Method.POST;
            apiRequest.Route = ApiRouteHelper.GetRoute(ApiRoute.Registor);
            apiRequest.Parameters = input;
           var result= await httpRestClient.Execute<LoginInputDTO, User>(apiRequest);
            if (result.ResultCode == 101) SelectedIndex = 0;
               eventAggregator.GetEvent<MsgEvent>().Publish(result.Msg);

        }
        private void TogglePage()
        {
            SelectedIndex = SelectedIndex == 0 ? 1 : 0; 
        }
        private void CloseWindow()
        {
            RequestClose?.Invoke(new DialogResult(ButtonResult.No));
        }
    
        #endregion
        #region 属性
        public string Title { get; set; } = "我的日常";

        private int selectedIndex = 0;

        public int SelectedIndex
        {
            get { return selectedIndex; }
            set { selectedIndex = value;RaisePropertyChanged(); }
        }
        private string accountName="1027559897@qq.com";

        public string AccountName
        {
            get { return accountName; }
            set { accountName = value; RaisePropertyChanged(); }
        }
        private string pwd;
        public string Pwd
        {
            get { return pwd; }
            set { pwd = value; RaisePropertyChanged(); }
        }

        private LoginInputDTO loginInputDTO;

        public LoginInputDTO LoginInputDTOProp
        {
            get { return loginInputDTO; }
            set { loginInputDTO = value; RaisePropertyChanged(); }
        }
        #endregion

        #region 弹窗处理
        public event Action<IDialogResult> RequestClose;

        public bool CanCloseDialog()
        {
            return true;
        }

        public void OnDialogClosed()
        {

        }

        public void OnDialogOpened(IDialogParameters parameters)
        {

        }
        #endregion
    }
}
